﻿#if UNITY_EDITOR

using UnityEditor;
using UnityEngine;

namespace AI.Graph.Editor {


    [CustomEditor (typeof (SubState), true)]
    public class SubStateView : CollapsibleNodeView<SubState> {

        private float activeStateValue;


        protected sealed override Vector2 OnDrawNode (bool drawSimple) {
            if (drawSimple) {
                Rect inputRect = DrawInput (0);

                Rect headerRect = DrawNodeHeader (inputRect.yMax - 4);

                Rect progressRect = DrawActiveStateAnimation (headerRect.yMax, node.activeTimer.isActive, ref activeStateValue);

                Rect spacingRect = DrawSpaceArea (progressRect.yMax, 20);

                EditorGUI.DrawSolidTexture (spacingRect, Styles.outlineColor.color, new BorderWidth (0, 0, 1, 0));

                Rect footerRect = DrawNodeFooter (spacingRect.yMax);

                return new Vector2 (180, footerRect.yMax);
            }
            else {
                Rect inputRect = DrawInput (0);

                Rect headerRect = DrawNodeHeaderWithToggle (inputRect.yMax - 4, ref node.isCollapsed);

                Rect progressRect = DrawActiveStateAnimation (headerRect.yMax, node.activeTimer.isActive, ref activeStateValue);

                Rect spacingRect = DrawSpaceArea (progressRect.yMax, 20);

                EditorGUI.DrawSolidTexture (spacingRect, Styles.outlineColor.color, new BorderWidth (0, 0, 1, 0));

                Rect collapseRect = DrawNodeCollapseArea (spacingRect.yMax);

                Rect footerRect = DrawNodeFooter (collapseRect.yMax);

                return new Vector2 (300, footerRect.yMax);
            }
        }


        /// <summary>
        /// Draws the input area.
        /// </summary>
        /// <param name="y">The start point.</param>
        /// <returns>Returns the size of the draw area.</returns>
        Rect DrawInput (float y) {
            Rect r = new Rect (0, y, node.size.x, 45);

            EditorGUI.DrawSolidTexture (r, Styles.background300.color, new BorderRadius (4, 4, 0, 0));

            Vector2 portPosition = new Vector2 (node.position.x + r.center.x, node.position.y + 8);

            DrawConnectionPort (node.input, portPosition, Alignment.MiddleTop, LabelPosition.Bottom);

            EditorGUI.DrawSolidTexture (r, Styles.outlineColor.color, new BorderWidth (Styles.outlineWidth, Styles.outlineWidth, Styles.outlineWidth, 0), new BorderRadius (4, 4, 0, 0));

            return r;
        }


        internal override EdgePoint GetEdgePointForPort (ConnectionPort port) {
            return new EdgePoint (port.rect.center, Vector2.up, colors.primaryColor2, colors.primaryColor1);
        }

    }


}
#endif